import tkinter as tk
from tkinter import messagebox
import threading


class PopupGenerator:
    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("200x120")
        self.root.title("无限弹窗")
        self.root.protocol("WM_DELETE_WINDOW", lambda: None)  # 用户不可关闭弹窗一
        self.common_style = {"font": ("华文新魏", 14)}
        self.label = tk.Label(self.root, text="恭喜你打开了这个程序", **self.common_style, fg="red")
        self.label.pack(pady=20)
        self.close_program_button = tk.Button(self.root, text="关闭程序", command=self.try_detox, **self.common_style,
                                              bg="green", fg="white")
        self.close_program_button.pack(pady=10)
        self.popup_count = 0
        self.detox_attempts = 0
        self.popup_positions = []  # 存储已存在弹窗的位置信息
        self.generate_popup()


    def gg(self,index):
        popup = tk.Toplevel(self.root)
        popup.title(str(index))
        popup.geometry("200x120")
        # 检查已存在弹窗的位置，设置新弹窗的位置
        x, y = self.calculate_popup_position(popup.winfo_reqwidth(), popup.winfo_reqheight())
        popup.geometry(f"+{x}+{y}")
        popup_label = tk.Label(popup, text="多试一下", fg="blue", **self.common_style)
        popup_label.pack(pady=20)
        self.popup_count += 1
        threading.Timer(1, self.generate_popup).start()

    def generate_popup(self):
        if self.popup_count < 20:
            self.gg(self.popup_count)

    def calculate_popup_position(self, width, height):
        # 计算新弹窗的位置，避免重叠
        x_offset, y_offset = 25, 25
        x = self.root.winfo_x() + x_offset + len(self.popup_positions) * x_offset
        y = self.root.winfo_y() + y_offset + len(self.popup_positions) * y_offset
        # 存储新弹窗的位置信息
        self.popup_positions.append((x, y))
        return x, y






if __name__ == "__main__":
    popup_generator = PopupGenerator()
    popup_generator.root.mainloop()